package com.dmwork.service.interaction.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.interaction.entity.WorkCollect;
import com.dmwork.service.interaction.entity.vo.WorkCollectAddVo;
import com.dmwork.service.interaction.entity.vo.WorkCollectMoveVo;
import com.dmwork.service.interaction.entity.vo.WorkCollectRemoveVo;
import com.dmwork.service.interaction.mq.sender.MessageSender;
import com.dmwork.service.interaction.service.WorkCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-03-20
 */
@Slf4j
@Api(description = "收藏管理")
@Transactional
@RestController
@RequestMapping("/front/dm_work/interaction/collect/me")
public class FrontMeWorkCollectController {

    @Autowired
    private WorkCollectService workCollectService;
    @Autowired
    private MessageSender messageSender;


    @ApiOperation("添加收藏并指定多个收藏分组")
    @PostMapping("add_batch_collect")
    public R addBatchCollect(
            HttpServletRequest request,
            @ApiParam(value = "作品收藏信息", required = true) @RequestBody WorkCollectAddVo workCollectAddVo) {

        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        workCollectService.addBatchCollect(workCollectAddVo, jwtToken.getId());
        messageSender.sendWorkCollectCountChangeMessage(workCollectAddVo.getWorkId(), 1, new HashMap<>());
        return R.ok().message("收藏成功");
    }

    @ApiOperation("将指定作品移除出所在收藏分组")
    @DeleteMapping("remove_from_collect")
    public R removeFromCollect(
            HttpServletRequest request,
            @ApiParam(value = "收藏信息", required = true) @RequestBody WorkCollect workCollect) {

        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        workCollect.setCustomerId(jwtToken.getId());
        //移除前先统计该用户所有收藏分组中该作品的数量，如果大于1就只移除出当前分组，若等于1说明只在当前分组有该作品，移除的同时取消收藏
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", jwtToken.getId());
        int count = workCollectService.count(queryWrapper);
        if (count == 1) { //移除的同时完全取消收藏，作品的收藏数减少
            Integer collectCount = workCollectService.getCollectCountByWorkId(workCollect.getWorkId());
            Map<String, Integer> collectCountMap=new HashMap<>();
            collectCountMap.put(workCollect.getWorkId(), collectCount);
            messageSender.sendWorkCollectCountChangeMessage(collectCountMap, new HashMap<>());
        }
        workCollectService.removeCollect(workCollect);
        return R.ok().message("移除成功");
    }

    @ApiOperation("批量取消收藏")
    @DeleteMapping("remove_batch_from_collect")
    public R removeBatchCollect(HttpServletRequest request, @RequestBody WorkCollectRemoveVo workCollectRemoveVo) {
        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        Map<String, Integer> collectCountMap = workCollectService.getBatchCollectCountByWorkIdList(workCollectRemoveVo.getWorkIdList());
        workCollectService.removeBatchCollect(jwtToken.getId(), workCollectRemoveVo.getCollectGroupId(), workCollectRemoveVo.getWorkIdList());
        messageSender.sendWorkCollectCountChangeMessage(collectCountMap, new HashMap<>());
        return R.ok().message("取消收藏成功");
    }

    @ApiOperation("批量移动作品到收藏分组")
    @PutMapping("move_batch_collect")
    public R moveBatchCollect(HttpServletRequest request, @RequestBody WorkCollectMoveVo workCollectMoveVo) {
        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        workCollectService.moveBatchCollect(jwtToken.getId(), workCollectMoveVo.getCollectGroupId(), workCollectMoveVo.getOriginalGroupId(), workCollectMoveVo.getWorkIdList());
        return R.ok().message("修改成功");
    }

    @ApiOperation("获取用户指定收藏分组的作品数")
    @GetMapping("collect_count/{collectGroupId}")
    public R getCollectGount(
            HttpServletRequest request,
            @ApiParam(value = "收藏分组id", required = true) @PathVariable("collectGroupId") String collectGroupId) {

        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", jwtToken.getId())
                .eq("collect_group_id", collectGroupId);
        int count = workCollectService.count(queryWrapper);
        return R.ok().data("count", count);
    }

    @ApiOperation("查看作品是否已被收藏")
    @GetMapping("check_work_is_collected/{workId}")
    public R checkWorkIsCollected(HttpServletRequest request, @PathVariable("workId") String workId) {
        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", jwtToken.getId())
                .eq("work_id", workId);
        int count = workCollectService.count(queryWrapper);
        if (count > 0) {
            return R.ok().data("collected", true);
        } else {
            return R.ok().data("collected", false);
        }
    }


}

